﻿using Orleans;
using Orleans.Runtime.Configuration;
using Pay.Cache;
using Pay.Configuration;
using Pay.Container;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Pay.Api
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);


            // 初始化配置
            Config.Init(ConfigurationManager.AppSettings["config:path"]);

            // 初始化redis连接
            RedisConnection.Init(Config.GetValue("redis:connection"), Config.GetValue("redis:key"));

            //依赖注入
            LocManager.Register();

            InitOrleanClient();
        }

        private void InitOrleanClient()
        {
            try
            {
                var config = ClientConfiguration.LoadFromFile(Server.MapPath(@"~/App_Data/ClientConfiguration.xml"));
                GrainClient.Initialize(config);
            }
            catch
            {
                Thread.Sleep(200);
                InitOrleanClient();
            }
        }
    }
}
